import { UseInfo_Store } from '@/stores/index'
import axios from 'axios'
import router from '@/router'

const baseURL = 'http://127.0.0.1:8080/api/v1'

const instance = axios.create({                  
    baseURL: baseURL, 
    timeout: 1000
})
//请求拦截器
instance.interceptors.request.use(
  (config) => {                                                   
    const useInfo = UseInfo_Store()
    if(useInfo.token){
        config.headers.Authorization = 'Bearer ' + useInfo.token
    }
    return config
  },
  (err) => Promise.reject(err),
)
//响应拦截器
instance.interceptors.response.use(
  (res) => {
    console.log("响应拦截器：：： ",res)
    if(res.data.code === 100){
        return res.data
    }
    ElMessage.error(res.data.msg || '服务异常')
    return Promise.reject(res.data)
  },
  (err) => {
    if(err.response?.data?.code === 400){                      
        router.push('/')
    }
    ElMessage.error(err.data.msg || '服务异常')
    return Promise.reject(err)
  }
)

export default instance
